<?php
namespace app\api\controller;
use think\Controller;
use app\common\lib;
use think\Loader;
use think\File;
use think\Image;
use think\Request;

class Upload extends Controller
{
   public function change_logo()
    {
        $isLogin = lib\Tools::isLogin();
        if(!$isLogin) {
            return show('500','请登录后操作');
        }
        $file = request()->file('file');
        $dir = './logo/';
        $info = $file->move($dir);
        if($info && $info->getPathname()) {
            $_res = Model('Config')->save(['value'=>$info->getSaveName()],['name'=>'logo']);
            if(!$_res) {
                return show('500', '数据库错误，上传失败');
            }
            return show('200', '上传成功！');
        }else {
            return show('500', '上传失败');
        }
        return show('500', '上传失败');
    }

    public function banner()
    {
        $isLogin = lib\Tools::isLogin();
        if(!$isLogin) {
            return show('500','请登录后操作');
        }
        $file = request()->file('file');
        $dir =  './banner/';
        $info = $file->move($dir);
        if($info && $info->getPathname()) {
            return show('200',$info->getSaveName());
        }else {
            return show('500', '上传失败');
        }
        return show('500', '上传失败');
    }

    /**
     * json(['code' => 0, 'msg' => '', 'data' => $data]);
     * {
     *   "code": 0 //0表示成功，其它失败
     *   ,"msg": "" //提示信息 //一般上传失败后返回
     *   ,"data": {
     *       "src": "图片路径",
     *       "title": "图片名称" //可选
     *      }
     *  }
     */
    public function edit_img()
    {
        $isLogin = lib\Tools::isLogin();
        if(!$isLogin) {
            return show('500','请登录后操作');
        }
        $file = request()->file('file');
        $dir = './about/';
        $info = $file->move($dir);
        if($info && $info->getPathname()) {
            $data = [
                'src'    => '__STATIC__/../about/'.$info->getSaveName(),
                'title'  => 'link_image'
            ];
            return json(['code' => 0, 'msg' => 'ok', 'data' => $data]);
        }else {
            return json(['code' => 500, 'msg' => 'error', 'data' => '']);
        }
        return json(['code' => 500, 'msg' => 'error', 'data' => '']);
    }

    public function car_upload()
    {
        include EXTEND_PATH.'AipOcr.php';
        // 你的 APPID AK SK
        $APP_ID = '';
        $API_KEY = '';
        $SECRET_KEY = '';
        $client = new \AipOcr($APP_ID, $API_KEY, $SECRET_KEY);

        $isLogin = lib\Tools::isIndexLogin();
        if(!$isLogin) {
            return show('500','请登录后操作','0');
        }
        $file = \request()->file('file');
        $dir =  './car/';
        $info = $file->move($dir);
        $data = array();
        if($info && $info->getPathname()) {

            $path = './car/'.$info->getSaveName();
            $path = $this->thumb_img($path);
            // 如果有可选参数
            $options = array();
            $options["multi_detect"] = "false";
            $image = file_get_contents($path);
            // 调用车牌识别
            $result = $client->licensePlate($image,$options);
            if(isset($result['error_msg'])){
//                $this->error('识别不到图中车辆，请重新上传');
                return show('500','识别不到图中车辆，请重新拍摄','0');
            }
            $data['car_thumb'] = './car/'.$info->getSaveName();
            $data['car_number'] = $result['words_result']['number'];
//            $data['car_number'] = $result['error_code'];
            return show('200','上传成功！',$data);
        }else {
            return show('500', '上传失败');
        }
    }

    public function thumb_img($url)
    {
        $image = Image::open($url);
        // 返回图片的宽度
        $width = $image->width();
        // 返回图片的高度
        $height = $image->height();
        $new_name = './car/'.time().'.jpg';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
        $image->thumb($width*0.08,$height*0.08)->save($url);
        if(is_ios() == 'IOS' || is_ios() == 'iPad') {
            $image->rotate(90)->save($url);
        }elseif (is_ios() == 'Android') {
            $image->rotate(90)->save($url);
//            $image->rotate(270)->save($url);
        }
        return $url;
    }
}